home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 008 / egademo.arc / STAR_CHT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-12  |  11.4 KB  |  402 lines

  1. /* spacerot.c  bru  03/02/1986 17:06:23 */
  2. /* stdio.h */
  3. typedef int  FILE;
  4.  
  5. #define stdin   0
  6. #define stdout  1
  7. #define stderr  2
  8. #define stdprn  4
  9.  
  10. #define NULL    0
  11. #define TRUE    1
  12. #define FALSE   0
  13. #define EOF     (-1)
  14. #define ERR     (-1)
  15.  
  16. #include <math.h>
  17.  
  18. #define  LOCAL     auto
  19. #define  COORSIZE  4536
  20. #define pi 3.14159   /*used to convert degrees to radians*/
  21.  
  22. /* structure of coordinate database */
  23. struct {     int    rec_c;
  24.              char   color_c;
  25.              char   size_c;
  26.              int    x_raw;
  27.              int    y_raw;
  28.              int    z_raw;
  29.              char   tic_one;
  30.              char   tic_two;  }  *starcoor;
  31.  
  32. /* global variables */
  33. float d_x,d_y,d_z;              /* display coordinates of viewed objects */
  34. int rot_x,rot_y,rot_z;        /* rotation in degrees for a normal vector */
  35. int ship_x,ship_y,ship_z;     /* current ship coordinates */
  36. double trans_x[4][4];          /* x coordinate transformation array     */
  37. double trans_y[4][4];          /* y coordinate transformation array     */
  38. double trans_z[4][4];          /* z coordinate transformation array     */
  39. char *bcoor;                  /* beginning of coordinate file */
  40. char *ecoor;                  /* end of coordinate file */
  41. unsigned page,color;
  42. int backxy[100][2];           /* list of background stars */
  43. int blackxy[2][100][3];       /* page,stars,xy coord, dia, for blacking out */
  44.  
  45. main()
  46. {
  47.   init_var();           /* initalize var, load arrays + memory */
  48.   draw_stars();         /* put current set of stars on screen  */
  49.   /* a couple of trial loops */
  50.   for (rot_x = 1; rot_x <= 245; x_rot_inc(10) ) draw_stars();
  51.   draw_stars();
  52.   draw_stars();
  53.   draw_stars();
  54.   for (rot_y = 1; rot_y <= 245; y_rot_inc(10) ) draw_stars();
  55.   draw_stars();
  56. /*for (rot_z = 1; rot_z <= 245; z_rot_inc(50) ) draw_stars();
  57.   draw_stars();
  58. */ /* ending stuff */
  59.   show_buffer(0);
  60.   set_write_buffer(0);
  61.   clear_color();
  62.   set_video_mode(0x03);
  63.  
  64. }
  65.  
  66. void init_var()  /* initialize variables, build arrays */
  67. {
  68.   load_coor();
  69.   set_video_mode(0x10);
  70.   page = 0;
  71.   init_backstars();
  72.   ship_x = 1;
  73.   ship_y = 1;
  74.   ship_z = 1;
  75.   rot_x = 10;
  76.   rot_y = 45;
  77.   rot_z = 90;
  78.   x_rot_inc(0);
  79.   y_rot_inc(0);
  80.   z_rot_inc(0);
  81. }
  82.  
  83. /* load coordinates, initialize pointers */
  84. void load_coor()
  85. {
  86.   LOCAL char *cp;
  87.   LOCAL int *fp;
  88.   starcoor = bcoor = malloc ( COORSIZE );
  89.   ecoor = bcoor + COORSIZE ;
  90.   fp = fopen( "starcoor.dat", "r" );
  91.   cp = bcoor;
  92.   while ( cp != ecoor )
  93.     {
  94.       /* problem here, if 26 is fetched, a 255 is put */
  95.       *cp = getc( fp );
  96.       ++cp;
  97.     }
  98.   fclose("starcoor.dat");
  99. }
  100.  
  101. void init_backstars()  /* builds array of background stars */
  102. {
  103.   srand(89);
  104.   int srow,scol,i;
  105.   double drow,dcol,dval;
  106.   color = 8;
  107.   for(i=0;i<100;i++)
  108.   {
  109.     dval =  frand();
  110.     drow = 350 * dval;
  111.     dval = frand();
  112.     dcol = 640 * dval;
  113.     srow = (int)(drow);
  114.     scol = (int)(dcol);
  115.     backxy[i][0]=scol-320;
  116.     backxy[i][1]=srow-175;
  117.   }
  118. }
  119.  
  120. void draw_backstars()  /* draws the background stars */
  121. {
  122.   int i;
  123.   color = 8;
  124.   for(i=0;i<100;i++) plotxy(backxy[i][0],backxy[i][1]);
  125. }
  126.  
  127. void page_black()  /* clear the page to black */
  128. {
  129.   int urb;
  130.   color = 0;
  131.   for (urb=0;urb<100;urb++)circle(blackxy[page][urb][0],
  132.                                   blackxy[page][urb][1],
  133.                                   blackxy[page][urb][2]);
  134. }
  135.  
  136. void transform_xyz()  /* transforms coordinates of viewed object */
  137. {
  138.   /* raw coord minus ships position divided by scaling factor */
  139.   d_x = (starcoor.x_raw - ship_x)/.5;
  140.   d_y = (starcoor.y_raw - ship_y)/.5;
  141.   d_z = (starcoor.z_raw - ship_z)/.5;
  142.   /* initialize global variables */
  143. /*  printf("Original Coord %d, %d, %d   Transformed coord %d, %d, %d\n",
  144.   starcoor.x_raw,starcoor.y_raw,starcoor.z_raw,d_x,d_y,d_z); */
  145. }
  146.  
  147. /* x_rot_inc    increment x axis rotation */
  148. /* acts on global rot_x, and always returns 0-360 degrees of rotation */
  149. x_rot_inc(i)
  150. int i; /* amount of rotation in degrees */
  151. {
  152.   double sx,cx; /* sine and cosine values */
  153.   double frx; /* float values in radians */
  154.   rot_x = rot_x + i;
  155.   if (rot_x >= 360) rot_x = rot_x - 360;
  156.   if (rot_x < 0) rot_x = rot_x +360;
  157.   /* fix up sin and cosine values */
  158.   /* convert degrees to radians    */
  159.   frx=pi*(rot_x/180.0);
  160.   /* calculate cosines and sines */
  161.   cx=cos(frx);
  162.   sx=sin(frx);
  163. /*  printf("X DegRot %d, RadRot %f, CosX %f, SinX %f\n",rot_x,frx,cx,sx); */
  164.   /* fixup transformation matrix   */
  165.   trans_x[1][1]=1.0;
  166.   trans_x[1][2]=0.0;
  167.   trans_x[1][3]=0.0;
  168.   trans_x[2][1]=0.0;
  169.   trans_x[2][2]=cx;
  170.   trans_x[2][3]=-sx;
  171.   trans_x[3][1]=0.0;
  172.   trans_x[3][2]=sx;
  173.   trans_x[3][3]=cx;
  174. }
  175.  
  176. /* y_rot_inc    increment y axis rotation */
  177. /* acts on global rot_y, and always returns 0-360 degrees of rotation */
  178. y_rot_inc(i)
  179. int i; /* amount of rotation in degrees */
  180. {
  181.   double sy,cy; /* sine and cosine values */
  182.   double fry; /* float values in radians */
  183.   rot_y = rot_y + i;
  184.   if (rot_y >= 360) rot_y = rot_y - 360;
  185.   if (rot_y < 0) rot_y = rot_y +360;
  186.   /* fix up sin and cosine values */
  187.   /* convert degrees to radians    */
  188.   fry=pi*(rot_y/180.0);
  189.   /* calculate cosines and sines */
  190.   cy=cos(fry);
  191.   sy=sin(fry);
  192. /*  printf("Y DegRot %d, RadRot %f, CosY %f, SinY %f\n",rot_y,fry,cy,sy);  */
  193.   /*fixup transformation matrix*/
  194.   trans_y[1][1]=cy;
  195.   trans_y[1][2]=0.0;
  196.   trans_y[1][3]=sy;
  197.   trans_y[2][1]=0.0;
  198.   trans_y[2][2]=1.0;
  199.   trans_y[2][3]=0.0;
  200.   trans_y[3][1]=-sy;
  201.   trans_y[3][2]=0.0;
  202.   trans_y[3][3]=cy;
  203. }
  204.  
  205. /* z_rot_inc    increment z axis rotation */
  206. /* acts on global rot_z, and always returns 0-360 degrees of rotation */
  207. z_rot_inc(i)
  208. int i; /* amount of rotation in degrees */
  209. {
  210.   double sz,cz; /* sine and cosine values */
  211.   double frz; /* float values in radians */
  212.   rot_z = rot_z + i;
  213.   if (rot_z >= 360) rot_z = rot_z - 360;
  214.   if (rot_z < 0) rot_z = rot_z +360;
  215.   /* fix up sin and cosine values */
  216.   /* convert degrees to radians    */
  217.   frz=pi*(rot_z/180.0);
  218.   /* calculate cosines and sines */
  219.   cz=cos(frz);
  220.   sz=sin(frz);
  221. /*  printf("Z DegRot %d, RadRot %f, CosZ %f, SinZ %f\n",rot_z,frz,cz,sz); */
  222.   /*setup transformation matrix*/
  223.   trans_z[1][2]=-sz;
  224.   trans_z[1][3]=0.0;
  225.   trans_z[2][1]=sz;
  226.   trans_z[2][2]=cz;
  227.   trans_z[2][3]=0.0;
  228.   trans_z[3][1]=0.0;
  229.   trans_z[3][2]=0.0;
  230.   trans_z[3][3]=1.0;
  231. }
  232.  
  233. /* x_rot.c    rotates point coordinates about the x-axis */
  234. void x_rot()
  235. {
  236.   double xx,yy,zz;        /* temporary x,y,z coordinates                 */
  237.   /* multiply coordinates by transformations */
  238.   xx = (trans_x[1][1]*d_x) + (trans_x[1][2]*d_y) + (trans_x[1][3]*d_z);
  239.   yy = (trans_x[2][1]*d_x) + (trans_x[2][2]*d_y) + (trans_x[2][3]*d_z);
  240.   zz = (trans_x[3][1]*d_x) + (trans_x[3][2]*d_y) + (trans_x[3][3]*d_z);
  241.   /* fix up the global coodinates of the viewed object */
  242.   d_x = (float)(xx);
  243.   d_y = (float)(yy);
  244.   d_z = (float)(zz);
  245.   return;
  246. }
  247.  
  248. /* y_rot.c   rotates point coordinates about the y-axis  */
  249. void y_rot()
  250. {
  251.   double xx,yy,zz;    /* temporary x,y,z coordinates    */
  252.   /*multiply coordinates by transformations*/
  253.   xx = (trans_y[1][1]*d_x) + (trans_y[1][2]*d_y) + (trans_y[1][3]*d_z);
  254.   yy = (trans_y[2][1]*d_x) + (trans_y[2][2]*d_y) + (trans_y[2][3]*d_z);
  255.   zz = (trans_y[3][1]*d_x) + (trans_y[3][2]*d_y) + (trans_y[3][3]*d_z);
  256.   d_x=(float)(xx);
  257.   d_y=(float)(yy);
  258.   d_z=(float)(zz);
  259.   return;
  260. }
  261.  
  262. /* z_rot.c  rotates point coordinates about the z-axis */
  263. void z_rot()
  264. {
  265.   double xx,yy,zz;    /* temporary x,y,z coordinates */
  266.   /*multiply coordinates by transformations*/
  267.   xx = (trans_z[1][1]*d_x) + (trans_z[1][2]*d_y) + (trans_z[1][3]*d_z);
  268.   yy = (trans_z[2][1]*d_x) + (trans_z[2][2]*d_y) + (trans_z[2][3]*d_z);
  269.   zz = (trans_z[3][1]*d_x) + (trans_z[3][2]*d_y) + (trans_z[3][3]*d_z);
  270.   d_x=(float)(xx);
  271.   d_y=(float)(yy);
  272.   d_z=(float)(zz);
  273.   return;
  274. }
  275.  
  276. void draw_stars()   /* draw the stars */
  277. {
  278.   int diameter;
  279.   /* set buffers */
  280.   if (page == 0)
  281.   {
  282.     show_buffer(0);
  283.     set_write_buffer(1);
  284.     page = 1;
  285.   }else{
  286.     color = 8;
  287.     plotxy(0,-1);
  288.     plotxy(0,-2);
  289.     plotxy(0,0);
  290.     plotxy(0,1);
  291.     plotxy(0,2);
  292.     plotxy(-2,0);
  293.     plotxy(-1,0);
  294.     plotxy(1,0);
  295.     plotxy(2,0);
  296.     show_buffer(1);
  297.     set_write_buffer(0);
  298.     page = 0;
  299.   }
  300.   /* blacken a new page, put background stars in place */
  301.   page_black();
  302.   draw_backstars();
  303.   starcoor = bcoor;
  304.   int bru;
  305.   bru = 0;
  306.   while (starcoor < ecoor)
  307.   {
  308.     transform_xyz();     /* change positions relative to ships position */
  309. /*    printf(" Coord before rotation %d, %d, %d\n",d_x,d_y,d_z);
  310. */    x_rot();
  311.     y_rot();
  312.     z_rot();
  313. /*    printf(" Coord after rotation %d, %d, %d\n",d_x,d_y,d_z);
  314. */    /* don't bother with what you can't see anyway */
  315.     if (d_z >= 0 && d_y > -175 && d_y < 175 && d_x > -320 && d_x < 320 )
  316.     {
  317.         color = 4;
  318.         diameter = 8;
  319.         if ( starcoor.color_c == 77 ) color = 4;
  320.         if ( starcoor.color_c == 79 ) color = 1;
  321.         if ( starcoor.color_c == 75 ) color = 6;
  322.         if ( starcoor.color_c == 65 ) color = 7;
  323.         if ( starcoor.color_c == 66 ) color = 9;
  324.         if ( starcoor.color_c == 70 ) color = 10;
  325.         if ( starcoor.color_c == 71 ) color = 14;
  326.         if ( starcoor.size_c == 49 ) diameter = 8;
  327.         if ( starcoor.size_c == 50 ) diameter = 7;
  328.         if ( starcoor.size_c == 51 ) diameter = 6;
  329.         if ( starcoor.size_c == 52 ) diameter = 5;
  330.         if ( starcoor.size_c == 53 ) diameter = 4;
  331.         if ( starcoor.size_c == 54 ) diameter = 3;
  332. /*        printf("  To Circle x %d, y %d, radius %d\n",d_x,d_y,diameter);
  333. */        if ( starcoor.size_c == 55 ) diameter = 2;
  334.         int dx,dy;
  335.         dx=(int)(d_x);
  336.         dy=(int)(d_y);
  337.         circle(dx,dy,diameter);
  338.         blackxy[page][bru][0] = dx;
  339.         blackxy[page][bru][1] = dy;
  340.         blackxy[page][bru][2] = diameter;
  341.         bru++;
  342.      }
  343.     starcoor++;
  344.   }
  345. }
  346.  
  347. circle(xc,yc,radius)   /* draws a circle */
  348. int xc,yc,radius;
  349. {
  350. /*  printf("  in CIRCLE x %d, y %d, radius %d\n",xc,yc,radius);
  351. */  if (radius > 0)
  352.   {
  353.   int xcd,ycd,a,b,f;
  354.   xcd=radius;
  355.   ycd=0;
  356.   a=-2*(xcd+1);
  357.   b=1;
  358.   f=0;
  359.   do{
  360. /*     printf(" p8 xc=%d, yc=%d, xcrd=%d, ycrd=%d\n",xc,yc,xcd,ycd);
  361. */     plotxy( ycd+xc,((5* xcd)/6)+yc);  /* b quart c-clockwise   */
  362.      plotxy(-ycd+xc,((5* xcd)/6)+yc);  /* b quart clockwise     */
  363.      plotxy(-ycd+xc,((5*-xcd)/6)+yc);  /* d quart c-clockwise   */
  364.      plotxy( ycd+xc,((5*-xcd)/6)+yc);  /* d quart clockwise     */
  365.      plotxy( xcd+xc,((5*-ycd)/6)+yc);  /* a quart c-clockwise   */
  366.      plotxy( xcd+xc,((5* ycd)/6)+yc);  /* a quart clockwise     */
  367.      plotxy(-xcd+xc,((5* ycd)/6)+yc);  /* c quart c-clockwise   */
  368.      plotxy(-xcd+xc,((5*-ycd)/6)+yc);  /* c quart clockwise     */
  369.      ycd=ycd+1;
  370.      f=f+b;
  371.      if ( f > radius )
  372.      {
  373.        f=f+a;
  374.        a=a+2;
  375.        xcd=xcd-1;
  376.      }
  377.      b=b+2;
  378.     }
  379.      while(b<=-a);
  380.   }
  381. }
  382.  
  383. plotxy(pcol,prow)  /* plots points on the screen */
  384. int pcol,prow;
  385. {
  386. /*  printf("  in plot px %d, py %d",pcol,prow);
  387. */  int ncol,nrow;
  388.   unsigned  ux,uy;
  389.   ncol = pcol + 320; /* change from screen center 0,0 to */
  390.   nrow = prow + 175; /* lower left 0,0 coordinates */
  391. /*  printf("  calcu (+320)ncol %d, (+175)nrow %d\n",ncol,nrow);
  392. */  if ( nrow <= 349 && nrow >= 0 && ncol >= 0 && ncol <= 639 )
  393.   {
  394.   set_color(color);
  395.   ux=(unsigned)(ncol);
  396.   uy=(unsigned)(nrow);
  397. /*  printf("   plotting ncol %d,  nrow %d\n",ncol,nrow);
  398. */  c_write_dot(ux,uy);
  399.   }
  400. }
  401.  
  402.